package troisdimension.client;

import com.google.gwt.dom.client.Element;
import com.google.gwt.user.client.DOM;

public class transformation {
    public static double  z = 0;
	
	public transformation() {
	}
	
	public PointCentral transformer(PointCentral ptC){
		double xangle = ptC.getDegreX() * (Math.PI / 180);
		double yangle = ptC.getDegreY() * (Math.PI / 180);
		double zangle = ptC.getDegreZ() * (Math.PI / 180);

		
		for (Point pt :  ptC.getListePoint()) {
			projection3Dimension( pt , xangle ,  yangle ,  zangle);
			 projection2Dimension( pt , ptC);
		
			
		}
	
		
		return ptC;
	}
	
	public static void projection3Dimension(Point pt ,double xangle , double yangle , double zangle){
		double ypostemp = pt.getyR() * Math.cos(xangle) + pt.getzR() * Math.sin(xangle);
		double zpostemp = -pt.getyR() * Math.sin(xangle) + pt.getzR() * Math.cos(xangle);
		double xpostemp = pt.getxR() * Math.cos(yangle) - zpostemp * Math.sin(yangle);
		double zpos = pt.getxR() * Math.sin(yangle) + zpostemp * Math.cos(yangle);
		double xpos = xpostemp * Math.cos(zangle) + ypostemp * Math.sin(zangle);
		double ypos = -xpostemp * Math.sin(zangle) + ypostemp * Math.cos(zangle);
		
		pt.setxAngle0(xpos);
		pt.setyAngle0(ypos);
		pt.setzAngle0(zpos);
			
	}
	
	
	public static void projection2Dimension(Point pt , PointCentral ptC){	
			pt.setProfondeur(ptC.getFocal() / (ptC.getFocal() + pt.getzAngle0() + ptC.getzR()));
			pt.setX2d(Math.round(pt.getxAngle0() * pt.getProfondeur() + ptC.getxR()));
			pt.setY2d(Math.round(pt.getyAngle0() * pt.getProfondeur() + ptC.getyR()));	
	}
	
	
	public static Point getPosition3d(double x , double y ,double profondeur, PointCentral ptC){
		Point pt = new Point();
		double xangle = ptC.getDegreX() * (Math.PI / 180);
		double yangle = ptC.getDegreY() * (Math.PI / 180);
		double zangle = ptC.getDegreZ() * (Math.PI / 180);

		double zpos = ptC.getzR() - profondeur;
		profondeur = 1;
		
		double xpos = x / profondeur - ptC.getxR();
		double ypos = y / profondeur - ptC.getyR();
		
		// d1 = a1x + b1y 
		// d2 = a2x + b2y		
		// xpos = xpostemp * Math.cos(zangle) + ypostemp * Math.sin(zangle);
		// ypos = -xpostemp * Math.sin(zangle) + ypostemp * Math.cos(zangle);		
		// x = (b2d1 - b1d2)/(a1b2 - a2b1)
		// y = (a1d2 - a2d1)/(a1b2 - a2b1)		
		double xpostemp = (xpos * Math.cos(zangle) - ypos * Math.sin(zangle)) / (Math.cos(zangle) * Math.cos(zangle) -  Math.sin(zangle) *  Math.sin(zangle));
		double ypostemp = (ypos * Math.cos(zangle) - xpos * Math.sin(zangle)) / (Math.cos(zangle) * Math.cos(zangle) -  Math.sin(zangle) *  Math.sin(zangle));
		
		// d1 = a1x + b1y 
		// d2 = a2x + b2y		
		// xpostemp = pt.getxR() * Math.cos(yangle) - zpostemp * Math.sin(yangle);
		// zpos = pt.getxR() * Math.sin(yangle) + zpostemp * Math.cos(yangle);	
		// x = (b2d1 - b1d2)/(a1b2 - a2b1)
		// y = (a1d2 - a2d1)/(a1b2 - a2b1)			
		double xR = (-xpostemp * Math.cos(yangle) - zpos * Math.sin(yangle)) / (Math.cos(yangle) * Math.cos(yangle) -  Math.sin(yangle) *  Math.sin(yangle)); 		
		double zpostemp = (zpos * Math.cos(yangle) - xpostemp * Math.sin(yangle)) / (Math.cos(yangle) * Math.cos(yangle) -  Math.sin(yangle) *  Math.sin(yangle));		
		
		// d1 = a1x + b1y 
		// d2 = a2x + b2y		
		// ypostemp = pt.getyR() * Math.cos(xangle) + pt.getzR() * Math.sin(xangle);
		// zpostemp = -pt.getyR() * Math.sin(xangle) + pt.getzR() * Math.cos(xangle);
		// x = (b2d1 - b1d2)/(a1b2 - a2b1)
		// y = (a1d2 - a2d1)/(a1b2 - a2b1)	
		double yR = (ypostemp * Math.cos(xangle) - zpostemp * Math.sin(xangle)) / (Math.cos(xangle) * Math.cos(xangle) -  Math.sin(xangle) *  Math.sin(xangle));
		double zR = (zpostemp * Math.cos(xangle) - ypostemp * Math.sin(xangle)) / (Math.cos(xangle) * Math.cos(xangle) -  Math.sin(xangle) *  Math.sin(xangle));

		pt.setXA(ptC.getxR() - xR  );
		pt.setYA(yR - ptC.getyR());
		pt.setZA(zR  - ptC.getzR());

		projection3Dimension( pt , 0 ,  0 ,  0);
		
		
		return pt;
	}
	
	  public Element getElementById(String id) {
	      final Element result = DOM.getElementById(id);
	      if (null == result) {
	          throw new IllegalArgumentException(
	                  "element with id " + id + " not found");
	      }
	      return result;
	  }
	 

}
